package com.dtda.edu.miaosha.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dtda.edu.miaosha.bo.ItemBo;
import com.dtda.edu.miaosha.bo.MarketBo;
import com.dtda.edu.miaosha.dao.MarketPoMapper;
import com.dtda.edu.miaosha.po.MarketPo;
import com.dtda.edu.miaosha.service.ActivityService;
import com.dtda.edu.miaosha.service.ItemService;

/**
 * @author 袁强
 * @version 2019年3月25日 下午5:19:45
 * 类说明  
 */
@Service
public class ActivityServiceimpl implements ActivityService {

	@Autowired
	private MarketPoMapper marketPoMapper;
	@Autowired
	private ItemService itemService;
	@Override
	public List<ItemBo> showActivities() {
		// TODO Auto-generated method stub
		List<ItemBo> list = new ArrayList<ItemBo>();
		List<MarketPo> marketPos = marketPoMapper.selectActivity();
		if(marketPos!=null) {
			for (MarketPo marketPo : marketPos) {
				Integer itemId = marketPo.getItemId();
				ItemBo itemBo = itemService.showItemById(itemId);
				MarketBo marketBo = new MarketBo();
				BeanUtils.copyProperties(marketPo, marketBo);
				marketBo.setMarketItemPrice(BigDecimal.valueOf(marketPo.getMarketItemPrice()));
				itemBo.setMarketBo(marketBo);
				list.add(itemBo);
			}
			return list;
		}else {
			return null;
		}
	}
	@Override
	public boolean deleteActivity(Integer id) {
		// TODO Auto-generated method stub
		int i = marketPoMapper.deleteByPrimaryKey(id);
		if(i>0) {
			return true;
		}else {
			return false;
		}
	}
	@Override
	public boolean updateActivity(MarketBo marketBo) {
		// TODO Auto-generated method stub
		MarketPo marketPo = new MarketPo();
		BeanUtils.copyProperties(marketBo, marketPo);
		marketPo.setMarketItemPrice(marketBo.getMarketItemPrice().doubleValue());
		int i = marketPoMapper.updateByPrimaryKeySelective(marketPo);
		if(i>0) {
			return true;
		}else {
			return false;
		}
	}
	@Override
	public ItemBo showActivityByItemId(String itemId) {
		// TODO Auto-generated method stub
		//MarketPo marketPo = marketPoMapper.selectByPrimaryKey(Integer.valueOf(id));
		ItemBo itemBo = itemService.showItemById(Integer.valueOf(itemId));
		if(itemBo!=null) {
			return itemBo;
		}
		return null;
	}
	@Override
	public int insertActivity(MarketBo marketBo) {
		// TODO Auto-generated method stub
		MarketPo marketPo = new MarketPo();
		BeanUtils.copyProperties(marketBo, marketPo);
		marketPo.setMarketItemPrice(marketBo.getMarketItemPrice().doubleValue());
		int i = marketPoMapper.insertSelective(marketPo);
		return i;
	}
}